﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLTTTH.Entity;
using QLTTTH.Config;
using System.Data;
using System.Data.SqlClient;

namespace QLTTTH.DAO
{
    class KenhDAO:DAOBase
    {
        public DataSet GetAll()
        {
            return Select("select * from Kenh");
        }

        public Kenh GetById(String maKenh)
        {
            Kenh k = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("select * from Kenh where MaKenh = '" + maKenh + "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    k = new Kenh();
                    k.MaKenh = dataReader.GetString(0);
                    k.TenKenh = dataReader.GetString(1);
                    return k;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();

            }
            return k;
        }

        public bool Insert(Kenh k)
        {

            String sql = "INSERT INTO [QLVT].[dbo].[Kenh]([MaKenh],[TenKenh])"
               + "VALUES('" + k.MaKenh + "'," + k.TenKenh + ")";
            return Update(sql);
        }

        public bool DeleteById(String makenh)
        {
            return Update("delete from Kenh where MaKenh = '" + makenh + "'");
        }

        public bool UpdateById(Kenh k)
        {
            String sql = "update Kenh "
                 + "set TenKenh = '" + k.TenKenh + "'"
                 + " where MaKenh =  '" + k.MaKenh + "'";
            return Update(sql);
        }
    }
}
